<template>
  <div :style="'width:' + width">
    <div class="form-item-label-top">
      <span style="color: red; margin-right: 5px" v-if="required">*</span>
      <div>{{ name }}</div>
    </div>
    <el-table :data="data.emps" stripe border highlight-current-row>
      <el-table-column type="index" width="40"></el-table-column>
      <el-table-column :label="cmpName" v-if="!inner">
        <template slot-scope="scope">
          {{ scope.row.cmpName }}
        </template>
      </el-table-column>
      <el-table-column label="税号" v-if="!inner">
        <template slot-scope="scope">
          {{ scope.row.cmpTaxNo }}
        </template>
      </el-table-column>
      <el-table-column :label="empName">
        <template slot-scope="scope">
          {{ scope.row.empName }}
        </template>
      </el-table-column>
      <el-table-column label="联系方式">
        <template slot-scope="scope">
          {{ scope.row.empPhone }}
        </template>
      </el-table-column>
      <el-table-column width="80px">
        <template slot="header">
          <el-button size="medium" type="text" slot="reference" icon="el-icon-plus" @click="addEmployeeVis = true"
            :disabled="disabled"></el-button>
        </template>
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" @click="
            employee = scope.row
          editEmployeeVis = true
            " :disabled="disabled"></el-button>
          <el-button type="text" icon="el-icon-delete" @click="removeEmployee(scope.$index)"
            :disabled="disabled"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="addEmployeeVis" width="500px" :title="'新增' + empName" :modal-append-to-body="false">
      <el-form label-width="60px" label-position="right">
        <el-form-item label="公司" v-if="!inner">
          <el-select style="width: 100%" v-model="employee.cmpId" filterable remote reserve-keyword
            placeholder="请输入员工公司进行搜索" :remote-method="getCompanies" :loading="loading" :disabled="employee.isNew"
            @change="chooseCompany">
            <el-option v-for="item in companies" :key="item.CMP_ID" :label="item.CMP_Name"
              :value="item.CMP_ID"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工">
          <div style="display: flex">
            <el-select style="flex: 1" v-model="employee.empId" filterable remote reserve-keyword
              placeholder="请输入员工姓名搜索" :disabled="employee.isNew" @change="chooseEmployee">
              <el-option v-for="item in employees" :key="item.ECV_TargetID" :label="item.USR_Name"
                :value="item.ECV_TargetID">
                【{{ item.USR_Name }}】{{ item.USR_Phone | encodePhone }}
              </el-option>
            </el-select>

            <el-button size="small" v-if="allowNew" :icon="employee.isNew ? 'el-icon-minus' : 'el-icon-plus'"
              @click="isNew()"></el-button>
          </div>
        </el-form-item>
        <el-form-item label="公司" v-if="employee.isNew && !inner">
          <el-input style="width: 100%" v-model="employee.cmpName" placeholder="请输入公司名" @input="
            employee.cmpId = null
          employee.empId = null
            "></el-input>
        </el-form-item>
        <el-form-item label="税号" v-if="employee.isNew && !inner">
          <el-input style="width: 100%" v-model="employee.cmpTaxNo" placeholder="请输入税号" @input="
            employee.cmpId = null
          employee.empId = null
            "></el-input>
        </el-form-item>
        <el-form-item label="姓名" v-if="employee.isNew">
          <el-input style="width: 100%" v-model="employee.empName" placeholder="请输入员工姓名"
            @input="employee.empId = null"></el-input>
        </el-form-item>
        <el-form-item label="手机" v-if="employee.isNew">
          <el-input style="width: 100%" v-model="employee.empPhone" placeholder="请输入员工手机号码"
            @input="employee.empId = null"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addEmployee">新增</el-button>
          <el-button type="text" @click="addEmployeeVis = false">
            取消
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="editEmployeeVis" width="500px" :title="'修改' + empName" :modal-append-to-body="false"
      @close="input">
      <el-form label-width="60px" label-position="right">
        <el-form-item label="公司" v-if="!inner">
          <el-select style="width: 100%" v-model="employee.cmpId" filterable remote reserve-keyword
            placeholder="请输入员工公司进行搜索" :remote-method="getCompanies" :loading="loading" :disabled="employee.isNew"
            @change="chooseCompany">
            <el-option v-for="item in companies" :key="item.CMP_ID" :label="item.CMP_Name"
              :value="item.CMP_ID"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工">
          <div style="display: flex">
            <el-select style="flex: 1" v-model="employee.empId" filterable remote reserve-keyword
              placeholder="请输入员工姓名搜索" :disabled="employee.isNew" @change="chooseEmployee">
              <el-option v-for="item in employees" :key="item.ECV_TargetID" :label="item.USR_Name"
                :value="item.ECV_TargetID">
                【{{ item.USR_Name }}】{{ item.USR_Phone | encodePhone }}
              </el-option>
            </el-select>

            <el-button size="small" v-if="allowNew" :icon="employee.isNew ? 'el-icon-minus' : 'el-icon-plus'"
              @click="isNew()"></el-button>
          </div>
        </el-form-item>
        <el-form-item label="公司" v-if="employee.isNew && !inner">
          <el-input style="width: 100%" v-model="employee.cmpName" placeholder="请输入公司名" @input="
            employee.cmpId = null
          employee.empId = null
            "></el-input>
        </el-form-item>
        <el-form-item label="代码" v-if="employee.isNew && !inner">
          <el-input style="width: 100%" v-model="employee.cmpTaxNo" placeholder="请输入统一税号" @input="
            employee.cmpId = null
          employee.empId = null
            "></el-input>
        </el-form-item>
        <el-form-item label="姓名" v-if="employee.isNew">
          <el-input style="width: 100%" v-model="employee.empName" placeholder="请输入员工姓名"
            @input="employee.empId = null"></el-input>
        </el-form-item>
        <el-form-item label="手机" v-if="employee.isNew">
          <el-input style="width: 100%" v-model="employee.empPhone" placeholder="请输入员工手机号码"
            @input="employee.empId = null"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import {
  getCompanies,
  getEmployees,
} from '@/api/flowCenter/field/employeeSelection'
export default {
  props: ['value', 'disabled', 'props', 'required', 'isBusiness'],
  data: function () {
    return {
      data: {
        emps: [],
        displayName: '',
      },
      employee: {
        empId: null,
        cmpId: null,
        cmpName: '',
        cmpTaxNo: '',
        empName: '',
        empPhone: '',
        isNew: false,
      },
      companies: [],
      employees: [],
      loading: false,
      addEmployeeVis: false,
      editEmployeeVis: false,
    }
  },
  computed: {
    ...mapGetters({
      curEmp: 'user/curEmp',
    }),
    myprops() {
      return this.props ? this.props : []
    },
    name() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'name')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value ? val.BFFP_Value : '员工列表'
        } else {
          return val.FFLP_Value ? val.FFLP_Value : '员工列表'
        }
      }
      return '员工列表'
    },
    width() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'width')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value ? val.BFFP_Value : '100%'
        } else {
          return val.FFLP_Value ? val.FFLP_Value : '100%'
        }
      }
      return '100%'
    },
    cmpName() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'cmpName')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value ? val.BFFP_Value : '公司'
        } else {
          return val.FFLP_Value ? val.FFLP_Value : '公司'
        }
      }
      return '公司'
    },
    empName() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'empName')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value ? val.BFFP_Value : '员工'
        } else {
          return val.FFLP_Value ? val.FFLP_Value : '员工'
        }
      }
      return '员工'
    },
    inner() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'inner')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value == 'true' || val.BFFP_Value == true
            ? true
            : false
        } else {
          return val.FFLP_Value == 'true' || val.FFLP_Value == true
            ? true
            : false
        }
      }
      return false
    },
    allowNew() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'allowNew')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value == 'true' || val.BFFP_Value == true
            ? true
            : false
        } else {
          return val.FFLP_Value == 'true' || val.FFLP_Value == true
            ? true
            : false
        }
      }
      return false
    },
  },
  methods: {
    isNew() {
      this.employee.isNew = !this.employee.isNew
      this.input()
    },
    addEmployee() {
      this.data.emps.push({ ...this.employee })
      this.addEmployeeVis = false
      this.input()
    },
    removeEmployee(index) {
      this.data.emps.splice(index, 1)
      this.input()
    },
    getCompanies(keyword) {
      if (keyword.length >= 2) {
        this.loading = true
        getCompanies({
          keyword,
        }).then(({ data }) => {
          this.companies = data.Items
          this.loading = false
        })
      }
    },
    chooseCompany(val) {
      let cmp = this.companies.find((x) => x.CMP_ID == val)
      this.employee.cmpId = val
      this.employee.cmpName = cmp.CMP_Name
      this.employee.cmpTaxNo = cmp.CMP_TaxNo
      getEmployees({
        cmpId: this.employee.cmpId,
      }).then(({ data }) => {
        this.employees = data
        this.loading = false
      })
    },
    chooseEmployee(val) {
      let emp = this.employees.find((x) => x.ECV_TargetID == val)
      this.employee.empId = val
      this.employee.empName = emp.USR_Name
      this.employee.empPhone = emp.USR_Phone
    },
    input() {
      this.$emit('input', JSON.stringify(this.data))
      if (this.data.emps) {
        let names = JSON.stringify(
          this.data.emps.map((x) => `${x.cmpName}${x.empName}`)
        )
        this.data.displayName = names
        this.$emit('display', this.data.displayName)
      }
    },
  },
  mounted: function () {
    if (this.value && Object.keys(this.value).length) {
      this.data = JSON.parse(this.value)
    }
    if (this.inner) {
      this.employee.cmpId = this.curEmp.CCV_TargetID
      getEmployees().then(({ data }) => {
        this.employees = data
      })
    }
  },
}
</script>
<style scoped>
.el-form-item {
  margin: 10px !important;
}
</style>